import 'package:hive/hive.dart';

part 'chat_model.g.dart';

@HiveType(typeId: 6)
class ChatModel extends HiveObject {
  @HiveField(0)
  String id;

  @HiveField(1)
  List<String> participantIds;

  @HiveField(2)
  bool isGroupChat;

  @HiveField(3)
  String lastMessageId;

  @HiveField(4)
  DateTime lastMessageTime;

  @HiveField(5)
  Map<String, int> unreadCountPerUser;

  @HiveField(6)
  String? groupName;

  @HiveField(7)
  String? groupAvatar;

  @HiveField(8)
  String? teamId;

  ChatModel({
    required this.id,
    required this.participantIds,
    this.isGroupChat = false,
    this.lastMessageId = '',
    required this.lastMessageTime,
    Map<String, int>? unreadCountPerUser,
    this.groupName,
    this.groupAvatar,
    this.teamId,
  }) : unreadCountPerUser = unreadCountPerUser ?? {};

  // 更新最后一条消息
  void updateLastMessage(String messageId, DateTime time) {
    lastMessageId = messageId;
    lastMessageTime = time;
  }

  // 增加某个用户的未读数
  void incrementUnreadCount(String userId) {
    unreadCountPerUser[userId] = (unreadCountPerUser[userId] ?? 0) + 1;
  }

  // 重置某个用户的未读数
  void resetUnreadCount(String userId) {
    unreadCountPerUser[userId] = 0;
  }
}
